package com.sushi.utils;


import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串相关 工具类
 * @author Administrator
 */
public class StringUtil {

    public static final String DIR_POINT = "\\.";

    /**
     * regex-英文句号
     */
    public static final String PERIOD = "\\.";

    /**
     * regex-英文逗号
     */
    public static final String COMMA = ",";

    /**
     * regex-英文感叹号
     */
    public static final String EXCLAMATION = "!";

    /**
     * regex-英文反斜杠
     */
    public static final String BACKSLASH = "\\\\";

    /**
     * 下划线
     */
    public static final String UNDERLINE = "_";
    public static final char UNDERLINE_CHAR = '_';

    /**
     * 字符串颠倒
     */
    public static String reverse(String str) {
        StringBuilder builder = new StringBuilder(str);
        return builder.reverse().toString();
    }

    /**
     * 字符串为空
     */
    public static boolean isBlank(String str) {
        return null == str || "".equals(str);
    }

    /**
     * 字符串不为空
     * @param str 需要判断的目标
     */
    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 俩字符串是否相等
     * @return 返回 是否一样
     */
    public static boolean equals(String str1, String str2) {
        if (null == str1) {
            return null == str2;
        }
        return str1.equals(str2);
    }

    /**
     * 合成字符串
     * @param list 数据来源
     * @param separator 分隔符
     */
    public static String join(List<String> list, String separator) {
        StringBuilder builder = new StringBuilder();
        if (ObjectUtil.isNotEmpty(list)) {
            list.forEach(s -> builder.append(s).append(separator));
        }
        int len = builder.length();
        if (len > 0) {
            builder.deleteCharAt(len - 1);
        }
        return builder.toString();
    }

    /**
     * 从字符串获取列表
     * @param source 资源，数据来源
     * @param separator 分隔符
     * @return 返回字符串列表
     */
    public static List<String> getList(String source, String separator) {
        String[] strings = source.split(separator);
        return Arrays.stream(strings).collect(Collectors.toList());
    }

    /**
     * 从字符串获取列表  默认英文逗号分隔
     * @param source 资源，数据来源
     * @return 返回字符串列表
     */
    public static List<String> getList(String source) {
        return getList(source, COMMA);
    }

    /**
     * 将字符串修饰成驼峰命名风格
     * @param underlineName 带下划线字符串
     * @param capitalized 首字母是否大写：ture-大写
     * @return 返回结果
     */
    public static String toCamelCaseStyle(final String underlineName, final boolean capitalized) {
        final StringBuilder builder = new StringBuilder();
        final int of = underlineName.indexOf((int) UNDERLINE_CHAR);
        // of > -1 表示找到位置
        if (of < 0) {
            if (capitalized) {
                return StringUtils.capitalize(underlineName);
            }
            else {
                return StringUtils.uncapitalize(underlineName);
            }
        }
        final char[] charArray = underlineName.toCharArray();
        //char[] resultArr = new char[charArray.length];
        boolean first = true;
        boolean underline = false;
        for (char ch : charArray) {
            if (ch == UNDERLINE_CHAR) {
                underline = true;
                continue;
            }
            // c >= 'A' && c <= 'Z'
            if (ch >= 65 && ch <= 90) {
                if (first) {
                    first = false;
                    underline = false;
                    if (!capitalized) {
                        ch = Character.toLowerCase(ch);
                    }
                }
                else if (underline) {
                    underline = false;
                    //c = Character.toLowerCase(c);
                }
            }
            // 等价 c >= 'a' && c <= 'z'
            if (ch >= 97 && ch <= 122) {
                if (first) {
                    first = false;
                    underline = false;
                    if (capitalized) {
                        ch = Character.toUpperCase(ch);
                    }
                }
                else if (underline) {
                    underline = false;
                    ch = Character.toUpperCase(ch);
                }
            }
            builder.append(ch);
        }
        return builder.toString();
    }


    /**
     * 将字符串修饰成 下划线拼接风格
     * @param source 要转换的字符串
     * @return 返回结果
     */
    public static String toUnderlineStyle(String source) {

        final StringBuilder builder = new StringBuilder();
        // 去除首尾空格
        source = source.trim();
        // 纯大写字符串 原样返回
        final String upperCase = source.toUpperCase();
        if (upperCase.equals(source)) {
            return source;
        }
        // 字符串内仅保留
        for (int i = 0, len = source.length(); i < len; i++) {
            final char c = source.charAt(i);
            // 如果是大写
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    builder.append(UNDERLINE);
                }
                builder.append(String.valueOf(c).toLowerCase());
            }
            else if (Character.isDigit(c) || Character.isLowerCase(c) || c == UNDERLINE_CHAR) {
                builder.append(Character.toLowerCase(c));
            }
        }
        return builder.toString();
    }


    /**
     * <p>Checks if a String {@code str} contains Unicode digits,
     * if yes then  return first  digits in {@code str} and return it as a String.</p>
     * <p>
     * <p>An empty ("") String will be returned if no digits found in {@code str}.</p>
     * <p>
     * <pre>
     * StringUtil.getFirstDigits(null)  = null
     * StringUtil.getFirstDigits("")    = ""
     * StringUtil.getFirstDigits("abc") = ""
     * StringUtil.getFirstDigits("1000$") = "1000"
     * StringUtil.getFirstDigits("1123~45") = "123"
     * StringUtil.getFirstDigits("0.125mm") = "0.125"
     * StringUtil.getFirstDigits("(541) 754-3010") = "541"
     * StringUtil.getFirstDigits("\u0967\u0968\u0969") = "\u0967\u0968\u0969"
     * </pre>
     *
     * @param str the String to extract digits from, may be null
     * @return String with only digits,
     * or an empty ("") String if no digits found,
     * or {@code null} String if {@code str} is null
     * @since 3.6
     */
    public static String getFirstDigit(final String str) {
        if (isBlank(str)) {
            return str;
        }
        if (str.contains("\\u")) {
            return "";
        }
        final int sz = str.length();
        final StringBuilder strDigits = new StringBuilder(sz);
        boolean flag = false;
        for (int i = 0; i < sz; i++) {
            final char tempChar = str.charAt(i);
            if (Character.isDigit(tempChar)) {
                flag = true;
                strDigits.append(tempChar);
            }
            else {
                if (flag) {
                    if ('.' == tempChar) {
                        strDigits.append(tempChar);
                    }
                    else {
                        break;
                    }
                }
            }
        }
        return strDigits.toString();
    }


}
